<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Happy dong
 * Created Time: 2019-06-26 15:31
 * Modified Time: 2019-06-26 15:31
 * Version: 2.0
 */

namespace App\Modules\Order\Criterias;

use Flower\Core\Repository\Contracts\CriteriaInterface;
use Flower\Core\Repository\Contracts\RepositoryInterface;

class AppointmentCriteria implements CriteriaInterface
{
	protected $data;

	public function __construct($storeId, $barberId, $date=null,$startTime=null,$endTime = null)
	{
		$this->data = [
			'store_id'  => $storeId,
			'barber_id' => $barberId,
			'date'      => $date,
			'start_time' =>$startTime,
			'end_time' => $endTime
		];
	}

	public function apply($model, RepositoryInterface $repository)
	{
		$map = [
			'store_id'  => $this->data['store_id'],
			'barber_id' => $this->data['barber_id'],
		];
		if (isset($this->data['date'])) {
			// date 格式 Y-m-d
			$start = strtotime($this->data['date'] . '00:00:00');
			$end = strtotime($this->data['date'] . '23:59:59');
		} else {
			$start = strtotime($this->data['start_time'] . '00:00:00');
			$end = strtotime($this->data['end_time'] . '23:59:59');
		}
		return $model->where($map)->where('status', '<>', 2)->where('appointment_time', '>', $start)->where('appointment_time', '<', $end);
	}


}